package com.message.tcp.ip.ui.account.panel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.message.tcp.ip.socket.configure.service.BinService;
import com.message.tcp.ip.ui.account.action.CardAccountAction;
import com.message.tcp.ip.ui.member.dialog.AccountTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CardAccountPanel extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField cardNumber;
	private JTextField balanceField;
	private JTextField expireDateField;
	private JTextField cavField;
	private JComboBox<String> binBox;
	private JComboBox<String> amtTypeBox;
	private JComboBox<String> currencyBox;
	private JComboBox<String> flagBox;
	private BinService binService;
	
	/**
	 * Create the dialog.
	 */
	public CardAccountPanel(AccountTable table) {
		setTitle("Card Account Registration");
		setBounds(100, 100, 450, 318);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{0, 76, 163, 0, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel binLabel = new JLabel("BIN");
			binLabel.setHorizontalAlignment(SwingConstants.TRAILING);
			GridBagConstraints gbc_binLabel = new GridBagConstraints();
			gbc_binLabel.anchor = GridBagConstraints.EAST;
			gbc_binLabel.insets = new Insets(0, 0, 5, 5);
			gbc_binLabel.gridx = 1;
			gbc_binLabel.gridy = 1;
			contentPanel.add(binLabel, gbc_binLabel);
		}
		{
			ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
			binService = (BinService) context.getBean("binService");
			String[] bs = binService.getBinList();
			String[] bins = new String[bs.length+1];
			bins[0] = "";
			for(int i=0;i<bs.length;i++)
				bins[i+1] = bs[i];
			binBox = new JComboBox<>(bins);
			GridBagConstraints gbc_binBox = new GridBagConstraints();
			gbc_binBox.insets = new Insets(0, 0, 5, 5);
			gbc_binBox.fill = GridBagConstraints.HORIZONTAL;
			gbc_binBox.gridx = 2;
			gbc_binBox.gridy = 1;
			contentPanel.add(binBox, gbc_binBox);
		}
		{
			JLabel requireLabel1 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel1 = new GridBagConstraints();
			gbc_requireLabel1.insets = new Insets(0, 0, 5, 0);
			gbc_requireLabel1.gridx = 3;
			gbc_requireLabel1.gridy = 1;
			contentPanel.add(requireLabel1, gbc_requireLabel1);
		}
		{
			JLabel cardNumberLabel = new JLabel("CARD");
			GridBagConstraints gbc_cardNumberLabel = new GridBagConstraints();
			gbc_cardNumberLabel.anchor = GridBagConstraints.EAST;
			gbc_cardNumberLabel.insets = new Insets(0, 0, 5, 5);
			gbc_cardNumberLabel.gridx = 1;
			gbc_cardNumberLabel.gridy = 2;
			contentPanel.add(cardNumberLabel, gbc_cardNumberLabel);
		}
		{
			cardNumber = new JTextField();
			cardNumber.setDocument(new JTextFieldLimit(16));
			binBox.addActionListener(new BinBoxAction(cardNumber,binBox));
			GridBagConstraints gbc_cardNumber = new GridBagConstraints();
			gbc_cardNumber.insets = new Insets(0, 0, 5, 5);
			gbc_cardNumber.fill = GridBagConstraints.HORIZONTAL;
			gbc_cardNumber.gridx = 2;
			gbc_cardNumber.gridy = 2;
			contentPanel.add(cardNumber, gbc_cardNumber);
			cardNumber.setColumns(10);
		}
		{
			JLabel requireLabel2 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel2 = new GridBagConstraints();
			gbc_requireLabel2.insets = new Insets(0, 0, 5, 0);
			gbc_requireLabel2.gridx = 3;
			gbc_requireLabel2.gridy = 2;
			contentPanel.add(requireLabel2, gbc_requireLabel2);
		}
		{
			JLabel amtTypeLabel = new JLabel("Amount Type");
			GridBagConstraints gbc_amtTypeLabel = new GridBagConstraints();
			gbc_amtTypeLabel.anchor = GridBagConstraints.EAST;
			gbc_amtTypeLabel.insets = new Insets(0, 0, 5, 5);
			gbc_amtTypeLabel.gridx = 1;
			gbc_amtTypeLabel.gridy = 3;
			contentPanel.add(amtTypeLabel, gbc_amtTypeLabel);
		}
		{
			String[] amtTypies = {"","01","02"};
			amtTypeBox = new JComboBox<>(amtTypies);
			GridBagConstraints gbc_amtTypeBox = new GridBagConstraints();
			gbc_amtTypeBox.insets = new Insets(0, 0, 5, 5);
			gbc_amtTypeBox.fill = GridBagConstraints.HORIZONTAL;
			gbc_amtTypeBox.gridx = 2;
			gbc_amtTypeBox.gridy = 3;
			contentPanel.add(amtTypeBox, gbc_amtTypeBox);
		}
		{
			JLabel requireLabel3 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel3 = new GridBagConstraints();
			gbc_requireLabel3.insets = new Insets(0, 0, 5, 0);
			gbc_requireLabel3.gridx = 3;
			gbc_requireLabel3.gridy = 3;
			contentPanel.add(requireLabel3, gbc_requireLabel3);
		}
		{
			JLabel currencyLabel = new JLabel("Currency");
			GridBagConstraints gbc_currencyLabel = new GridBagConstraints();
			gbc_currencyLabel.anchor = GridBagConstraints.EAST;
			gbc_currencyLabel.insets = new Insets(0, 0, 5, 5);
			gbc_currencyLabel.gridx = 1;
			gbc_currencyLabel.gridy = 4;
			contentPanel.add(currencyLabel, gbc_currencyLabel);
		}
		{
			String[] currencies = {"","418","840","764"};
			currencyBox = new JComboBox<>(currencies);
			GridBagConstraints gbc_currencyBox = new GridBagConstraints();
			gbc_currencyBox.insets = new Insets(0, 0, 5, 5);
			gbc_currencyBox.fill = GridBagConstraints.HORIZONTAL;
			gbc_currencyBox.gridx = 2;
			gbc_currencyBox.gridy = 4;
			contentPanel.add(currencyBox, gbc_currencyBox);
		}
		{
			JLabel requireLabel4 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel4 = new GridBagConstraints();
			gbc_requireLabel4.insets = new Insets(0, 0, 5, 0);
			gbc_requireLabel4.gridx = 3;
			gbc_requireLabel4.gridy = 4;
			contentPanel.add(requireLabel4, gbc_requireLabel4);
		}
		{
			JLabel flagLabel = new JLabel("Flag");
			GridBagConstraints gbc_flagLabel = new GridBagConstraints();
			gbc_flagLabel.anchor = GridBagConstraints.EAST;
			gbc_flagLabel.insets = new Insets(0, 0, 5, 5);
			gbc_flagLabel.gridx = 1;
			gbc_flagLabel.gridy = 5;
			contentPanel.add(flagLabel, gbc_flagLabel);
		}
		{
			String[] flags = {"","C","D"};
			flagBox = new JComboBox<>(flags);
			GridBagConstraints gbc_flagBox = new GridBagConstraints();
			gbc_flagBox.insets = new Insets(0, 0, 5, 5);
			gbc_flagBox.fill = GridBagConstraints.HORIZONTAL;
			gbc_flagBox.gridx = 2;
			gbc_flagBox.gridy = 5;
			contentPanel.add(flagBox, gbc_flagBox);
		}
		{
			JLabel requireLabel5 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel5 = new GridBagConstraints();
			gbc_requireLabel5.insets = new Insets(0, 0, 5, 0);
			gbc_requireLabel5.gridx = 3;
			gbc_requireLabel5.gridy = 5;
			contentPanel.add(requireLabel5, gbc_requireLabel5);
		}
		{
			JLabel balanceLabel = new JLabel("Balance");
			GridBagConstraints gbc_balanceLabel = new GridBagConstraints();
			gbc_balanceLabel.anchor = GridBagConstraints.EAST;
			gbc_balanceLabel.insets = new Insets(0, 0, 5, 5);
			gbc_balanceLabel.gridx = 1;
			gbc_balanceLabel.gridy = 6;
			contentPanel.add(balanceLabel, gbc_balanceLabel);
		}
		{
			balanceField = new JTextField();
			GridBagConstraints gbc_balanceField = new GridBagConstraints();
			gbc_balanceField.insets = new Insets(0, 0, 5, 5);
			gbc_balanceField.fill = GridBagConstraints.HORIZONTAL;
			gbc_balanceField.gridx = 2;
			gbc_balanceField.gridy = 6;
			contentPanel.add(balanceField, gbc_balanceField);
			balanceField.setColumns(10);
		}
		{
			JLabel requireLabel6 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel6 = new GridBagConstraints();
			gbc_requireLabel6.insets = new Insets(0, 0, 5, 0);
			gbc_requireLabel6.gridx = 3;
			gbc_requireLabel6.gridy = 6;
			contentPanel.add(requireLabel6, gbc_requireLabel6);
		}
		{
			JLabel expiryDateLabel = new JLabel("Expiry Date");
			GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
			gbc_lblNewLabel_7.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_7.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_7.gridx = 1;
			gbc_lblNewLabel_7.gridy = 7;
			contentPanel.add(expiryDateLabel, gbc_lblNewLabel_7);
		}
		{
			expireDateField = new JTextField();
			GridBagConstraints gbc_expireDateField = new GridBagConstraints();
			gbc_expireDateField.insets = new Insets(0, 0, 5, 5);
			gbc_expireDateField.fill = GridBagConstraints.HORIZONTAL;
			gbc_expireDateField.gridx = 2;
			gbc_expireDateField.gridy = 7;
			contentPanel.add(expireDateField, gbc_expireDateField);
			expireDateField.setColumns(10);
		}
		{
			JLabel requireLabel7 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel7 = new GridBagConstraints();
			gbc_requireLabel7.insets = new Insets(0, 0, 5, 0);
			gbc_requireLabel7.gridx = 3;
			gbc_requireLabel7.gridy = 7;
			contentPanel.add(requireLabel7, gbc_requireLabel7);
		}
		{
			JLabel cavLabel = new JLabel("CAV");
			GridBagConstraints gbc_lblNewLabel_8 = new GridBagConstraints();
			gbc_lblNewLabel_8.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_8.insets = new Insets(0, 0, 0, 5);
			gbc_lblNewLabel_8.gridx = 1;
			gbc_lblNewLabel_8.gridy = 8;
			contentPanel.add(cavLabel, gbc_lblNewLabel_8);
		}
		{
			cavField = new JTextField();
			GridBagConstraints gbc_cavField = new GridBagConstraints();
			gbc_cavField.insets = new Insets(0, 0, 0, 5);
			gbc_cavField.fill = GridBagConstraints.HORIZONTAL;
			gbc_cavField.gridx = 2;
			gbc_cavField.gridy = 8;
			contentPanel.add(cavField, gbc_cavField);
			cavField.setColumns(10);
		}
		{
			JLabel requireLabel8 = new JLabel("(require)");
			GridBagConstraints gbc_requireLabel8 = new GridBagConstraints();
			gbc_requireLabel8.gridx = 3;
			gbc_requireLabel8.gridy = 8;
			contentPanel.add(requireLabel8, gbc_requireLabel8);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				CardAccountAction action = new CardAccountAction(binBox,
						cardNumber, amtTypeBox, currencyBox, flagBox,
						balanceField, expireDateField, cavField, table, this);
				okButton.addActionListener(action);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	private class JTextFieldLimit extends PlainDocument {
		private static final long serialVersionUID = 1L;
		private int limit;

		public JTextFieldLimit(int limit) {
			super();
			this.limit = limit;
		}

		public void insertString(int offset, String str, AttributeSet attr)
				throws BadLocationException {
			if (str == null)
				return;

			if ((getLength() + str.length()) <= limit) {
				super.insertString(offset, str, attr);
			}
		}
	}
	
	private class BinBoxAction implements ActionListener{
		private JTextField binTextField;
		private JComboBox<String> box;
		public BinBoxAction(JTextField binTextField,JComboBox<String> box){
			this.binTextField = binTextField;
			this.box = box;
		}
		@Override
		public void actionPerformed(ActionEvent event) {
			String bin = (String) box.getSelectedItem();
			binTextField.setText(bin);
		}
		
	}
}
